package com.wangba.controller;

import com.wangba.pojo.TempUser;
import com.wangba.service.TempUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/tempUser")
public class TempUserController {

    @Autowired
    private TempUserService mermberService;

    @RequestMapping("/allTempUser")
    public String list(Model model) {
        List<TempUser> list = mermberService.queryAllTempUser();
        model.addAttribute("list", list);
        return "allTempUser";
    }

    @RequestMapping("/toAddTempUser")
    public String toAddPaper() {
        return "addTempUser";
    }

    @RequestMapping("/addTempUser")
    public String addPaper(TempUser tempUser) {
        System.out.println(tempUser);
        mermberService.addTempUser(tempUser);
        return "redirect:/tempUser/allTempUser";
    }

    @RequestMapping("/del/{tempUserbookID}")
    public String deleteTempUser(@PathVariable(value = "tempUserbookID") int id) {
        mermberService.deleteTempUserById(id);
        return "redirect:/tempUser/allTempUser";
    }

    @RequestMapping("/toUpdateTempUser")
    public String toUpdateTempUser(Model model, int id) {
        TempUser tempUser = mermberService.queryTempUserById(id);
        System.out.println(tempUser);
        model.addAttribute("tempUser", tempUser);
        return "updateTempUser";
    }

    @RequestMapping("/updateTempUser")
    public String updateTempUser(Model model, TempUser tempUser) {
        System.out.println(tempUser);
        TempUser tempUserSearch = mermberService.queryTempUserById(tempUser.getTempUserID());
        BigDecimal newCost = new BigDecimal(tempUserSearch.getTempUserCost()).add(new BigDecimal(tempUser.getTempUserCost()));
        tempUser.setTempUserCost(new DecimalFormat("##.##").format(newCost));

        mermberService.updateTempUser(tempUser);
        TempUser tempUserUpdate = mermberService.queryTempUserById(tempUser.getTempUserID());
        model.addAttribute("tempUser", tempUserUpdate);
        return "redirect:/tempUser/allTempUser";
    }

    @RequestMapping("/queryTempUser")
    public String querTempUser(String queryTempUserNo, Model model) {
        TempUser tempUser = mermberService.queryTempUserByNo(queryTempUserNo);
        List<TempUser> list = new ArrayList<TempUser>();
        list.add(tempUser);
        /*if (tempUser == null) {
            list = mermberService.queryAllTempUser();
            model.addAttribute("error", "未查到");
        }*/
        model.addAttribute("list", list);
        return "allTempUser";
    }


}





